<?php
	
class bn_post_custom
{
  
  private $dl;
  private $img;
  
  function __construct()
  {
    $this->dl=new bn_dal;
    $this->img=bn_images::getInstance();
  } 
  
  public function insert($post_id,$name,$value)
  {
    return $this->dl->insert('post_custom',func_get_args());
  } 
  
  public function delete($id)
  {
    return $this->dl->delete('post_custom',$id);
  }
  
  public function update($name,$value,$id)
  {
    return $this->dl->update('post_custom',array('value'=>$value),array('post_id='=>$id,'name='=>$name));
  }
  
  public function select($post_id,$name)
  {
    return $this->dl->getvalue('select value from post_custom where post_id=? and name=?',func_get_args());
  }
  
  public function set_post_password($post_id,$user,$pass)
  {
    if($this->dl->hasrow('select name from post_custom where name="user"'))
    {
        self::update('user',$user,$post_id);
        self::update('pass',$pass,$post_id);
    }
    else
    {
        self::insert($post_id,'user',$user);
        self::insert($post_id,'pass',$pass);
    }
  }
  
  public function get_post_password($post_id)
  {
    return $this->dl->select('select value from post_custom where post_id=? and name in("user","pass")',$post_id)->fetchCOL();
  }
  
  public function set_post_file($post_id,$files=array())
  {
    if(!empty($files))
    {
    $this->dl->query('delete from post_custom where post_id=? and name like "__files%"',$post_id);
    $cnt=1;
    foreach($files as $file)
    {
      self::insert($post_id,"__files$cnt",$file); 
      $cnt++; 
    }
    }
  }
  
  public function get_post_file($post_id)
  {
    return $this->dl->select('select value from post_custom where post_id=? and name like"__files%"',$post_id);
  }
  
  public function get_post_small_thumbnil($post_id)
  {
    $filename=self::select($post_id,'thumb');
    if(!empty($filename))
    return bn_smalthumb_path("$filename"); 
  }
  
    public function get_post_large_thumbnil($post_id)
  {
    $filename=self::select($post_id,'thumb');
    if(!empty($filename))
    return bn_largthumb_path("$filename"); 
  }
  
  public function set_post_thumbnil($post_id,$path)
  {
    if(!empty($path))
    {
        if($this->dl->hasrow('select name from post_custom where post_id=? and name="thumb"',$post_id))
        {
            $this->img->small_thumbnil($path);
            $this->img->large_thumbnil($path);
            self::update('thumb',basename($path),$post_id);
        }
        else
        {
            $this->img->small_thumbnil($path);
            $this->img->large_thumbnil($path);
            self::insert($post_id,'thumb',basename($path));
        }
    }
  }
  
  public function get_post_custom($post_id)
  {
    return $this->dl->select('select name,value from post_custom where post_id=? and name not in("thumb","user","pass") and name not like "__files%"',$post_id);
  }
  
  public function set_post_custom($post_id,$name=array(),$val=array())
  {
    if(!empty($name)&&!empty($val))
    {
    $name=array_values($name);
    $val=array_values($val);
    $this->dl->query('delete from post_custom where post_id=? and name not in("thumb","user","pass") and name not like "__files%"',$post_id);

    $cnt=count($name);
    for($i=0;$i<$cnt;$i++)
    {
      self::insert($post_id,$name[$i],$val[$i]);  
    }
    }
  }

public function post_file_table($post_id)
{
    $rows=self::get_post_file($post_id)->fetch();
    $table='';
    $cnt=1;
    if(!empty($rows))
    { global $lang;
    foreach($rows as $row)
    {
    $table.="\n<tr>"; 
    $table.="\n<td><input type='hidden' name='file[$cnt]' value='{$row['value']}'/>".$this->dl->getvalue('select m_filename from upload where filename="'.basename($row['value']).'"') ."</td>";
    $table.="\n<td><input type='submit' class='delete button red' value='".$lang[11]."'/></td>";
    $table.="\n</tr>";
    $cnt++; 
    }
    }
    return array($table,$cnt);
}

public function post_custom_table($post_id)
{
    $rows=self::get_post_custom($post_id)->fetch();
    $table='';
    $cnt=1;
    if(!empty($rows))
    {   global $lang;
        foreach($rows as $row)
        {
          $table.="\n<tr>
        \n<td><input type='text' size='25' name='name[$cnt]' value='{$row['name']}' class='input-text'/></td>
        \n<td><input type='text' size='25' name='val[$cnt]' value='{$row['value']}' class='input-text'/><td>
        \n<td><input type='submit' class='delete button red' value='$lang[11]'/><td>
        \n</tr>";
        $cnt++;  
        }
    }
    return array($table,$cnt);
}
}
?>